package com.crm.crmback.service.impl;

import com.crm.crmback.mapper.CourseOrderMapper;
import com.crm.crmback.model.CourseOrder;
import com.crm.crmback.model.MonthIncome;
import com.crm.crmback.model.PageResult;
import com.crm.crmback.service.ICourseOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CourseOrderServiceImpl implements ICourseOrderService {

    @Autowired
    CourseOrderMapper orderMapper;

    /**
     *
     * @param condition 查询条件
     * @param page 表示的是当前页
     * @param pageSize 每页显示多少条记录
     * @return
     */
    @Override
    public PageResult<CourseOrder> findPageResult(CourseOrder condition, int page, int pageSize) {
        //定义结果
        PageResult<CourseOrder> result = new PageResult<>();
        result.setCode(0);

        //定义一个 Map-参数先不放在 map,后面再处理
        Map<String,Object> params = new HashMap<>();
        params.put("condition", condition);
        params.put("start", (page-1)*pageSize);
        params.put("limit", pageSize);

        //下面是另外一种方式
//        params.put("wechat_no",condition.getWechat_no());
//        params.put("wechat_mark",condition.getWechat_mark());
//        params.put("qq_no",condition.getQq_no());
//        params.put("start",(page - 1) * pageSize);
//        params.put("limit",pageSize);

        //获取总记录数
        int totalCount = orderMapper.findCountByMap(params);
        List<CourseOrder> listByMap = orderMapper.findListByMap(params);
        //往pageResullt中存取数据
        result.setCount(totalCount);
        result.setData(listByMap);

        return result;
    }

    /**
     * 添加课程的方法
     * @param order
     */
    @Override
    public void save(CourseOrder order) {
        orderMapper.insert(order);
    }


    /**
     *
     * @param order_id 课程id
     * @return
     */
    @Override
    public CourseOrder findByOrderId(String order_id) {
        return orderMapper.findByOrderId(order_id);
    }

    /**
     * 根据课程id删除课程
     * @param order_id
     */
    @Override
    public void delete(String order_id) {
        orderMapper.delete(order_id);
    }

    /**
     * 根据id更新数据
     * @param order
     */
    @Override
    public void update(CourseOrder order) {
        orderMapper.update(order);
    }

    /**
     *
     * @return
     */
    @Override
    public List<MonthIncome> getMonthIcomes() {
        return orderMapper.getMonthIcomes();
    }


}
